#ifndef GUI_H
#define GUI_H

#include "opencv2/opencv.hpp"
#include <iostream>
#include <algorithm>

class Gui
{
public:
    Gui();

    /**
     * @brief  构造函数
     * @param 绘制画布
     * @param 镜头中心
     * @param 字体大小
     * @return 返回值
     * @autor  梁尧森
     * @date   2018-
     */
    Gui(cv::Mat img,cv::Point2f center,double digits_size,int status=0)
    {
        this->img=img.clone();
        this->center=center;
        this->digits_size=digits_size;
        this->status=status;

        cv::circle(this->img,cv::Point(center),3,cv::Scalar(0,255,0),1);
        cv::line(this->img,cv::Point(0,center.y),cv::Point(img.cols,center.y),cv::Scalar(0,127,0),1);
        cv::line(this->img,cv::Point(center.x,0),cv::Point(center.x,img.rows),cv::Scalar(0,127,0),1);
    }

    /**
     * @brief  实时绘制可视化信息
     * @param  画布
     * @param  装甲板四个顶点
     * @param  姿态角的三个坐标轴的单位向量，只传入顶点（1,0,0），（0,1,0），（0,0,1）
     * @param  pitch角
     * @param  yaw角
     * @param  距离
     * @return 返回值
     * @autor  梁尧森
     * @date   2018-
     */
    void drawingInfo(cv::Mat img,
                     std::vector<cv::Point2f> armor_rect,
                     std::vector<cv::Point2f> attitude_angle,
                     double pitch,
                     double yaw,
                     double dist)
    {
        //装甲板矩形角点排序
        std::sort(armor_rect.begin(),
                  armor_rect.end(),
                  [](cv::Point2f &p1,cv::Point2f &p2)->bool{return p1.y<p2.y;});
        std::sort(armor_rect.begin(),
                  armor_rect.end(),
                  [](cv::Point2f &p1,cv::Point2f &p2)->bool{return p1.x<p2.x;});

        cv::Point2f point_temp=armor_rect[2];
        armor_rect[2]=armor_rect[3];
        armor_rect[3]=point_temp;

        float x_sum=0.0;
        float y_sum=0.0;
        for(int i=0;i<armor_rect.size();i++)
        {
            if(status!=1)
            {
                //装甲板矩形
                cv::line(this->img,cv::Point(armor_rect[i]),cv::Point(armor_rect[(i+1)%4]),cv::Scalar(0,255,0),1);
                //装甲板角点
                cv::circle(this->img,cv::Point(armor_rect[i]),1,cv::Scalar(127,0,127),2);
            }
            x_sum+=armor_rect[i].x;
            y_sum+=armor_rect[i].y;
        }

        //装甲板中心点
        cv::Point2f armor_center=cv::Point2f(x_sum/4.0,y_sum/4.0);

        //姿态角三轴角点
        cv::circle(this->img,cv::Point(attitude_angle[0]),2,cv::Scalar(255,0,0),2);
        cv::circle(this->img,cv::Point(attitude_angle[1]),2,cv::Scalar(0,255,0),2);
        cv::circle(this->img,cv::Point(attitude_angle[2]),2,cv::Scalar(0,0,255),2);

        //姿态角三轴
        cv::line(this->img,cv::Point(armor_center),cv::Point(attitude_angle[0]),cv::Scalar(255,0,0),1);
        cv::line(this->img,cv::Point(armor_center),cv::Point(attitude_angle[1]),cv::Scalar(0,255,0),1);
        cv::line(this->img,cv::Point(armor_center),cv::Point(attitude_angle[2]),cv::Scalar(0,0,255),1);

        //装甲板中心点与光轴中心连线
        cv::line(this->img,cv::Point(armor_center),cv::Point(center),cv::Scalar(255,0,255),1);

        cv::Point2f dist_center=cv::Point2f((armor_center.x+center.x)/2.0,
                                            (armor_center.y+center.y)/2.0);

        //pitch角、yaw角、距离
        std::ostringstream text_pitch,text_yaw,text_dist;
        text_pitch<<pitch;
        text_yaw<<yaw;
        text_dist<<dist;
        cv::putText(this->img,text_pitch.str(),cv::Point(center.x,armor_center.y),cv::FONT_HERSHEY_COMPLEX,digits_size,cv::Scalar(0,255,0),1);
        cv::putText(this->img,text_yaw.str(),cv::Point(armor_center.x,center.y),cv::FONT_HERSHEY_COMPLEX,digits_size,cv::Scalar(0,255,0),1);
        cv::putText(this->img,text_dist.str(),cv::Point(dist_center),cv::FONT_HERSHEY_COMPLEX,digits_size,cv::Scalar(0,255,0),1);

        cv::namedWindow("drawing");
        cv::imshow("drawing",this->img);
    }

private:
    cv::Mat img;
    cv::Point2f center;
    double digits_size;
    int status;
};

#endif // GUI_H
